package com.jxdinfo.hussar.activitidemo.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxdinfo.hussar.activitidemo.dao.GetDeptIdDao;

/**
 * 查询单位信息实现类
 * 
 * @author zzd
 *
 */
@Service
public class GetDeptIdServiceImpl implements GetDeptIdService {
	@Autowired
	GetDeptIdDao dao;

	@Override
	public Map<String, Object> IsTiezByDeptId(String deptId) {
		Map<String, Object> remap = new HashMap<String, Object>();
		remap.put("mesg", "程序异常");
		remap.put("status", "error");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = dao.GetDeptNameByDeptId(deptId);
		} catch (NullPointerException e) {
			remap.put("mesg", "服务提供异常");
			remap.put("status", "error");
			return remap;
		} catch (Exception e) {
			remap.put("mesg", "程序异常");
			remap.put("status", "error");
			return remap;
		}
		String ORGAN_ALIAS = "", type = "", PARENT_ID = "";
		boolean flag = false;
		if (map != null) {
			PARENT_ID = map.get("PARENTID").toString();
			type = map.get("STRUTYPE").toString();
			ORGAN_ALIAS = map.get("ORGANALIAS").toString();
		} else {
			remap.put("status", "error");
			remap.put("mesg", "数据异常,未查到相关信息");
			return remap;
		}
		if ("1".equals(type)) {
			flag = false;
			remap.put("status", "success");
			remap.put("flag", flag);
			return remap;
		}
		if ("铁总".equals(ORGAN_ALIAS)) {
			flag = true;
			remap.put("status", "success");
			remap.put("flag", flag);
			return remap;
		} else {
			do {
				try {
					map = dao.GetDeptNameByDeptId(PARENT_ID);
				} catch (NullPointerException e) {
					remap.put("status", "error");
					remap.put("mesg", "服务提供异常");
					return remap;
				} catch (Exception e) {
					remap.put("status", "error");
					remap.put("mesg", "程序异常");
					return remap;
				}
				if (map != null) {
					PARENT_ID = map.get("PARENTID").toString();
					type = map.get("STRUTYPE").toString();
					ORGAN_ALIAS = map.get("ORGANALIAS").toString();
				} else {
					remap.put("status", "error");
					remap.put("mesg", "数据异常,未查到相关信息");
					return remap;
				}
				if ("1".equals(type)) {
					break;
				}
			} while (!"2".equals(type));
		}
		if ("铁总".equals(ORGAN_ALIAS)) {
			flag = true;
		} else {
			flag = false;
		}
		remap.put("status", "success");
		remap.put("flag", flag);
		return remap;
	}

	/**
	 * 根据登录人存储deptId信息获取所在部门id
	 */
	@Override
	public Map<String, Object> GetDeptIdByDeptId(String deptId) {
		Map<String, Object> remap = new HashMap<String, Object>();
		remap.put("mesg", "程序异常");
		remap.put("status", "error");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = dao.GetDeptNameByDeptId(deptId);
		} catch (NullPointerException e) {
			remap.put("mesg", "服务提供异常");
			remap.put("status", "error");
			return remap;
		} catch (Exception e) {
			remap.put("mesg", "程序异常");
			remap.put("status", "error");
			return remap;
		}
		String PARENT_ID = "", struType = "", struId = "";
		if (map != null) {
			PARENT_ID = map.get("PARENTID").toString();
			struType = map.get("STRUTYPE").toString();
			struId = map.get("STRUID").toString();
		} else {
			remap.put("mesg", "数据异常,未查到相关信息");
			remap.put("status", "error");
			return remap;
		}
		if ("1".equals(struType)) {
			remap.put("status", "success");
			remap.put("struId", struId);
			return remap;
		}
		if ("2".equals(struType)) {
			remap.put("status", "success");
			remap.put("struId", struId);
			return remap;
		} else {
			do {
				try {
					map = dao.GetDeptNameByDeptId(PARENT_ID);
				} catch (NullPointerException e) {
					remap.put("mesg", "服务提供异常");
					remap.put("status", "error");
					return remap;
				} catch (Exception e) {
					remap.put("mesg", "程序异常");
					remap.put("status", "error");
					return remap;
				}
				if (map != null) {
					PARENT_ID = map.get("PARENTID").toString();
					struType = map.get("STRUTYPE").toString();
					struId = map.get("STRUID").toString();
				} else {
					remap.put("mesg", "数据异常,未查到相关信息");
					remap.put("status", "error");
					return remap;
				}
				if ("1".equals(struType)) {
					break;
				}
			} while (!"2".equals(struType));
		}
		remap.put("status", "success");
		remap.put("struId", struId);
		return remap;
	}

	@Override
	public Map<String, Object> GetTodeptBydeptId(String deptId) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		try {
			reMap = dao.GetDeptNameByDeptId(deptId);

		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("msg", "查询信息失败");
			reMap.put("status", "error");
			return reMap;
		}
		return reMap;
	}
}
